// import { configureStore, createSlice } from '@reduxjs/toolkit';

// // 创建一个 slice（状态片段）
// const counterSlice = createSlice({
//   name: 'counter',
//   initialState: {
//     value: 0,
//   },
//   reducers: {
//     increment: (state) => {
//       state.value += 1;
//     },
//     decrement: (state) => {
//       state.value -= 1;
//     },
//     addBy: (state, action) => {
//       state.value += action.payload;
//     },
//    addstr:(state,action)=>{
//     state.value += action.payload; 
//    }

//   },
//    
 
// });

// // 导出 action creators
// export const { increment, decrement, addBy,addstr } = counterSlice.actions;

// // 创建 store
// export const store = configureStore({
//   reducer: {
//     counter: counterSlice.reducer,
//     // 其他的 reducer 函数...
//   },
// });



import { configureStore, createSlice, createAsyncThunk } from '@reduxjs/toolkit';
import axios from 'axios';

// 获取分页用户数据
export const fetchUserList = createAsyncThunk(
  'users/fetchUserList',
  async ({ page = 1, size = 10 }, thunkAPI) => {
    try {
      const response = await axios.get('http://127.0.0.1:3000/list', {
        params: { pages: page, size },
      });
      return response.data; // 假设接口返回的是数组
    } catch (error) {
      return thunkAPI.rejectWithValue(error.response?.data || '请求失败');
    }
  }
);

// 用户列表 Slice
const usersSlice = createSlice({
  name: 'users',
  initialState: {
    list: [],
    page: 1,
    size: 10,
    loading: false,
    error: null,
  },
  reducers: {
    setPage: (state, action) => {
      state.page = action.payload;
    },
  },
  extraReducers: (builder) => {
    builder
      .addCase(fetchUserList.pending, (state) => {
        state.loading = true;
        state.error = null;
      })
      .addCase(fetchUserList.fulfilled, (state, action) => {
        state.loading = false;
        state.list = action.payload;
      })
      .addCase(fetchUserList.rejected, (state, action) => {
        state.loading = false;
        state.error = action.payload || '加载失败';
      });
  },
});

// ✅ 导出 actions 和 reducer
export const { setPage } = usersSlice.actions;

export const store = configureStore({
  reducer: {
    users: usersSlice.reducer,
  },
});
